package com.ymatou.apigateway.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.OpenapiService;
import com.ymatou.apigateway.parameter.xloboGetStatusBean;
import com.ymatou.apigateway.service.xloboGetStatusCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*获取面单物流状态-A4格式面单
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_xloboGetStatus{
	private static OpenapiReq openapiReq;

	private static xloboGetStatusBean xlobogetstatusBean;
	private static xloboGetStatusCall xlobogetstatusCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取面单物流状态-A4格式面单");
	}

	@Before
	public void caseUp() {
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("xlobo.status.get");

		xlobogetstatusBean=new xloboGetStatusBean();
		xlobogetstatusCall=new xloboGetStatusCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(xlobogetstatusCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboGetStatus_001() {
		Logger.start(true,"获取面单物流状态");
		try {
	
			List<String> billCodes = new ArrayList<>();
			
			billCodes.add(EnvSetup.getData("billcode1"));    
			billCodes.add(EnvSetup.getData("billcode2"));
			billCodes.add(EnvSetup.getData("billcode3"));
			
			xlobogetstatusBean.setBillcodes(billCodes);
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xlobogetstatusBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.status.get",
							app_id);

			xlobogetstatusCall.setUrl(url);				

			xlobogetstatusCall.setData(openapiReq);
			xlobogetstatusCall.callService();			
			
			Logger.verifyEquals("0000", xlobogetstatusCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xlobogetstatusCall.getReturnData().contains("面单已生成"), "验证返回内容");
						
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboGetStatus_002() {
		Logger.start(true,"获取面单物流状态-billcodes空");
		try {
			List<String> billCodes = new ArrayList<>();
			
			xlobogetstatusBean.setBillcodes(billCodes);
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xlobogetstatusBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.status.get",
							app_id);

			xlobogetstatusCall.setUrl(url);				

			xlobogetstatusCall.setData(openapiReq);
			xlobogetstatusCall.callService();			
			
			Logger.verifyEquals("0001", xlobogetstatusCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xlobogetstatusCall.getReturnData().contains("billcodes:请求的面单号不能为空"), "验证返回内容");
						
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_xloboGetStatus_003() {
		Logger.start(true,"获取面单物流状态-billcodes不存在");
		try {
			List<String> billCodes = new ArrayList<>();
			
			billCodes.add("GH73389999444");    
			billCodes.add("GH789t999444");
			billCodes.add("GH7899994434");
					
			xlobogetstatusBean.setBillcodes(billCodes);
			
			long sellerId = Long.valueOf(EnvSetup.getData("xlobo_seller"));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			
			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(xlobogetstatusBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
		
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=xlobo.status.get",
							app_id);

			xlobogetstatusCall.setUrl(url);				

			xlobogetstatusCall.setData(openapiReq);
			xlobogetstatusCall.callService();			
			
			Logger.verifyEquals("0000", xlobogetstatusCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, xlobogetstatusCall.getReturnData().contains("[]"), "验证返回内容");
								
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}